package com.hktf.corelib.jsonbean;

import com.hktf.corelib.R;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.GsonUtils;
import com.hktf.corelib.utils.LogMds;
import com.hktf.corelib.utils.NetWorkUtils;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;

/**
 * date: 2022/9/19 15:48.
 *
 * @auther: cdz
 * @function:
 */
public abstract class BaseCallBack<T> implements Callback<T> {

    @Override
    public void onStart(Request<T, ? extends Request> request) {

    }

    @Override
    public void onSuccess(Response<T> response) {
        T body = response.body();
        if (body == null) {
            onError(new Exception("response.body=null"));
            return;
        }
        try {
            BaseResult result = (BaseResult) body;
            if (result.isSuccess()||result.isSuccessNew()) {
                onSUCCESS(response);
            } else {
                // TODO: 2022/9/19 错误情况
//                EventBusUtil.showOnResponseFail(result.getRESULT());
                Class<?> aClass = Class.forName("com.yx.uilib.engine.CommServiceExceptionHandler");
                Method handerServiceException = aClass.getDeclaredMethod("handerServiceException", String.class);
                handerServiceException.setAccessible(true);
                Object invoke = handerServiceException.invoke(null, result.getRESULT());
                if (invoke == null) {
                    onResponseFail("服务器返回错误码：" + result.getRESULT(),result.getRESULT());
                } else {
                    onResponseFail((String) invoke,result.getRESULT());
                }
            }
            return;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        onError(new Exception("服务器返回数据异常"));
    }

    public abstract void onSUCCESS(Response<T> response);

    public abstract void onError(Throwable exception);


    /**
     * 服务器返回错误码时调用 msg永不为空
     *
     * @param msg
     */
    public abstract void onResponseFail(String msg,String errorcode);

    @Override
    public void onCacheSuccess(Response<T> response) {

    }

    @Override
    public void onError(Response<T> response) {
        if (!NetWorkUtils.isConnectingToInternet(CommUtils.getContext())) {
            onResponseFail(CommUtils.getString(R.string.network_no_connected),"");
            return;
        }
        Throwable exception = response.getException();
        LogMds.i("cdz", "error:" + exception.getMessage());
        onError(response.getException());
    }

    @Override
    public void onFinish() {

    }

    @Override
    public void uploadProgress(Progress progress) {

    }

    @Override
    public void downloadProgress(Progress progress) {

    }

    @Override
    public T convertResponse(okhttp3.Response response) throws Throwable {
        ParameterizedType genericSuperclass = null;
        try {
            genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
            return GsonUtils.jsonToBean(response.body().string(), (Class<T>) genericSuperclass.getActualTypeArguments()[0]);
        } catch (Exception exception) {
            exception.printStackTrace();
            onError(exception);
        }
        return null;
    }
}
